
#import "AppDelegate+AppService.h"
#import "LoginViewController.h"
#import "BaseNavigationController.h"
#import "XFTarbarViewController.h"

@implementation AppDelegate (AppService)
#pragma mark ————— 初始化window —————
-(void)initWindow{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
}

//#pragma mark ————— 初始化用户系统 —————
-(void)jugdeLoginStatus{
    //    BaseNavigationController * Nav = [[BaseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
    //    self.window.rootViewController = Nav;
    
    //    XFTarbarViewController *tarBarVC =[XFTarbarViewController new];
    //    self.window.rootViewController = tarBarVC;
    //
    //    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //    [defaults removeObjectForKey:@"upDateVersonStatus"];
    //    NSString *key = versonString;
    //    //上一个版本号，存沙盒
    //    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    //    //当前版本号
    //    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    ////    LGFLog(@"lastVersion =%@ -- currentVersion = %@",lastVersion,currentVersion);
    //    if ([currentVersion isEqualToString:lastVersion]) {
    //        BaseNavigationController * Nav;
    //        if (![BookShareUserInforDefault readLoginStatus]) {
    ////            LGFLog(@"未登录");
    //            Nav = [[BaseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
    //            self.window.rootViewController = Nav;
    //        }else{
    ////            LGFLog(@"已登录");
    ////            XFTarbarViewController *tarBarVC =[XFTarbarViewController new];
    ////            self.window.rootViewController = tarBarVC;
    //        }
    //    }else{
    //        self.window.rootViewController = [LGFNewFeatureVC new];//为假表示没有文件，没有进入过主页
    //        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
    //        [[NSUserDefaults standardUserDefaults] synchronize];
    //    }
    
    BaseNavigationController * Nav;
    if (![BookShareUserInforDefault readLoginStatus]) {
        LGFLog(@"未登录");
        Nav = [[BaseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
        self.window.rootViewController = Nav;
    }else{
        LGFLog(@"已登录");
        XFTarbarViewController *tarBarVC = [XFTarbarViewController new];
        [tarBarVC login];
        self.window.rootViewController = tarBarVC;
    }
}

#pragma mark ————— 配置第三方 —————
-(void)configThirdParty{
    //设置键盘
    IQKeyboardManager *keyBoard = [IQKeyboardManager sharedManager];
    keyBoard.shouldResignOnTouchOutside = YES;
    keyBoard.toolbarDoneBarButtonItemText = @"确定";
    keyBoard.keyboardDistanceFromTextField = 40.0f;
    keyBoard.shouldShowTextFieldPlaceholder = NO;
    keyBoard.enableAutoToolbar = NO;
}

#pragma mark ————— OpenURL 回调 —————
+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

-(UIViewController *)getTopVC{
    
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
            if (tmpWin.windowLevel == UIWindowLevelNormal){
                window = tmpWin;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]){
        result = nextResponder;
    }else{
        result = window.rootViewController;
    }
    return result;
}

-(UIViewController *)getCurrentUIVC{
    
    UIViewController  *superVC = [self getTopVC];
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }else{
        if ([superVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)superVC).viewControllers.lastObject;
        }
        return superVC;
    }
}

@end

